//
//  LeadVC.m
//  DayClassroom
//
//  Created by haichuang on 2017/12/26.
//  Copyright © 2017年 王涛. All rights reserved.
//

#import "LeadVC.h"
#import "LoginViewController.h"
#import "MainTabBarController.h"
@interface LeadVC ()<UIScrollViewDelegate,UIGestureRecognizerDelegate,UITabBarControllerDelegate>

@property (strong, nonatomic)UIPageControl *pageControl;
@property (strong, nonatomic)NSArray *imageArr;

@end

@implementation LeadVC



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    _imageArr = @[@"幸福云岩",@"知行云岩",@"毓秀云岩"];
    [self createUI];
    //    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
    //    self.automaticallyAdjustsScrollViewInsets=NO;
    
}


-(void)createUI{
    
    self.view.backgroundColor=[UIColor whiteColor];
    UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, -20, ScreenWidth, self.view.frame.size.height+20)];
    scrollView.contentSize=CGSizeMake(ScreenWidth*(_imageArr.count+1), ScreenHeight);
    scrollView.pagingEnabled=NO;
    scrollView.userInteractionEnabled=YES;
    scrollView.showsHorizontalScrollIndicator=NO;
    scrollView.delegate=self;
    [self.view addSubview:scrollView];
    
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    //    if (_imageArr.count == 0) {
    //
    //        [self way];
    //    }
    
    for (int i=0; i<self.imageArr.count; i++) {
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth*i, 0, ScreenWidth, CGRectGetHeight(scrollView.frame))];
        //  imageView.image=[UIImage imageNamed:_imageArr[i]];
        
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",_imageArr[i]]];
        [scrollView addSubview:imageView];
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)];
        view.backgroundColor=[UIColor whiteColor];
        [imageView addSubview:view];
        
        if (i == _imageArr.count-1) {
            imageView.userInteractionEnabled = YES;
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
            [imageView addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo (self.view);
                make.height.equalTo (self.view.mas_height).multipliedBy (0.25);
                make.bottom.equalTo (self.view.mas_bottom).offset (-50.0f);
            }];
            
            [btn addTarget:self action:@selector(way) forControlEvents:UIControlEventTouchUpInside];
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
            
            btn.backgroundColor = [UIColor clearColor];
        }
    }
    _pageControl=[[UIPageControl alloc]init];
    _pageControl.hidden = YES;
    _pageControl.center=CGPointMake(ScreenWidth/2, ScreenHeight-90);
    [self.view addSubview:_pageControl];
    _pageControl.numberOfPages=_imageArr.count;
    //当前原点颜色
    _pageControl.currentPageIndicatorTintColor=[UIColor orangeColor];
    //其他原点颜色
    _pageControl.pageIndicatorTintColor=[UIColor whiteColor];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (scrollView.contentOffset.x<0) {
        scrollView.contentOffset=CGPointMake(0, 0);
        scrollView.pagingEnabled=NO;
    }
    if (scrollView.contentOffset.x>10) {
        scrollView.pagingEnabled=YES;
    }
    
    if (scrollView.contentOffset.x > (_imageArr.count - 1) * ScreenWidth) {
        scrollView.contentOffset = CGPointMake((_imageArr.count - 1) * ScreenWidth, 0);
        scrollView.pagingEnabled = NO;
    }
    CGFloat scrollviewW =  scrollView.frame.size.width;
    CGFloat x = scrollView.contentOffset.x;
    int page = (x + scrollviewW / 2) /  scrollviewW;
    self.pageControl.currentPage = page;
    
    //    if (page == 2) {
    //        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //            LoginViewController *loginVC = [LoginViewController new];
    //                UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:loginVC];
    //                [self presentViewController:navc animated:YES completion:nil];
    //        });
    //    }
}

-(void)way
{

    MainTabBarController * root = [[MainTabBarController alloc]init];
    [UIApplication sharedApplication].keyWindow.rootViewController = root;
}

@end

